package com.guigu.youxuan.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.mapper.SysUsersMapper;
import com.guigu.youxuan.pojo.SysUsers;
import com.guigu.youxuan.pojo.dto.LoginForm;
import com.guigu.youxuan.pojo.dto.RegisterForm;
import com.guigu.youxuan.service.ISysUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class SysUsersServiceImpl extends ServiceImpl<SysUsersMapper, SysUsers> implements ISysUsersService {
    @Autowired
    private SysUsersMapper sysUsersMapper;

    @Override
    public SysUsers getSysUsers(LoginForm loginForm) {
        QueryWrapper<SysUsers> queryWrapper = new QueryWrapper<>();
        //账号密码
        if(loginForm != null) {
            if(!StringUtils.isEmpty(loginForm.getUsername())) {
                queryWrapper.eq("username",loginForm.getUsername());
            }
            if(!StringUtils.isEmpty(loginForm.getPassword())) {
                queryWrapper.eq("password",loginForm.getPassword());
            }
        }
        //状态标志0 删除标志0
        queryWrapper.eq("state",1);
        queryWrapper.eq("is_deleted",0);

        SysUsers sysUsers = this.getOne(queryWrapper);

        return sysUsers;
    }

    @Override
    public String selUserCount(String username) {
        String sysUsers=sysUsersMapper.selUserCount(username);
        return sysUsers;
    }


    /**
     * 前台用户注册
     * @param sysUsers
     */
    @Override
    public void addSysUsers(SysUsers sysUsers) {
        if (sysUsers!=null)
            this.save(sysUsers);
    }

    @Override
    public PageUtils<SysUsers> getSysUsersList(SysUsers sysUsers, int pageNo, int pageSize) {
        QueryWrapper<SysUsers> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted", 0);
        if (sysUsers != null) {
            if (!StringUtils.isEmpty(sysUsers.getNickName())) {
                wrapper.like("nick_name", sysUsers.getNickName());
            }
            if (!StringUtils.isEmpty(sysUsers.getPhone())) {
                wrapper.eq("phone", sysUsers.getPhone());
            }
            if (!StringUtils.isEmpty(sysUsers.getRealName())) {
                wrapper.like("real_name", sysUsers.getRealName());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        PageHelper.orderBy("sys_users_id");
        List<SysUsers> list = sysUsersMapper.querySysAll(sysUsers);

        PageInfo<SysUsers> pageInfo = new PageInfo<>(list);

        PageUtils<SysUsers> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public void delSysUser(SysUsers sysUsers) {
        if (sysUsers != null && sysUsers.getSysUsersId() != null) {
            // 构建更新的值和条件
            UpdateWrapper<SysUsers> updateWrapper = new UpdateWrapper<>();

            // 设置要更新的列和新值
            updateWrapper.set("is_deleted", 1)
                    .eq("sys_users_id", sysUsers.getSysUsersId())
                    .eq("is_deleted", 0);
            // 确保仅更新未删除的记录

            // 执行更新操作
            this.update(sysUsers, updateWrapper);
        }
    }

    @Override
    public void delsSysUser(List<SysUsers> sysUsersList) {
        if (sysUsersList!= null &&!sysUsersList.isEmpty()) {
            // 构建更新的值和条件
            UpdateWrapper<SysUsers> updateWrapper = new UpdateWrapper<>();

            // 设置要更新的列和新值
            updateWrapper.set("is_deleted", 1)
                    .in("sys_users_id", sysUsersList.stream().map(SysUsers::getSysUsersId).toArray());

            // 执行更新操作
            this.update(updateWrapper);
        }
    }

    /**
     * 冻结/启用账户
     * @param sysUsers
     */
    @Override
    public void switchState(SysUsers sysUsers) {
        if(sysUsers!= null){
            this.updateById(sysUsers);
        }
    }

}
